import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/pages/devices/device_tab/device_detail/ems/rectangle_ems_romote_control/rectangle_operating_parameter_settings/rectangle_operating_parameter_settings_controller.dart';
import 'package:yt_devops/pages/devices/device_tab/device_detail/ems/rectangle_ems_romote_control/rectangle_operating_parameter_settings/rectangle_operating_parameter_settings_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';

// ignore: must_be_immutable
class RectangleOperateParameterSettingWidget
    extends GetView<RectangleOperateParameterSettingController> {
  RectangleOperateParameterSettingWidget({
    super.key,
    required this.deviceId,
  });

  int deviceId;
  final String _format = 'HH时:mm分';

  void _selectTime(BuildContext context, String key) {
    controller.initTime();
    controller.assignPickerTitle(key);
    controller.assignPickerTime(
      controller.initialStartDateTime,
      controller.initialEndDateTime,
    );

    BrnDateRangePicker.showDatePicker(
      context,
      pickerMode: BrnDateTimeRangePickerMode.time,
      minuteDivider: 1,
      pickerTitleConfig: BrnPickerTitleConfig(
        titleContent: controller.model.pickerTitle,
        confirm: Text(
          ResKey.sure.tr,
          style: TextStyle(
            color: Colour.primaryColor,
            fontSize: 16.sp,
          ),
        ),
      ),
      initialStartDateTime:
          controller.model.timeParameterMap[key]!.startDateTime,
      initialEndDateTime: controller.model.timeParameterMap[key]!.endDateTime,
      dateFormat: _format,
      onConfirm: (startDateTime, endDateTime, startlist, endlist) {
        controller.assignPickerTime(startDateTime, endDateTime);
        controller.validateTime(startDateTime, key);
        controller.assignFormTime(startDateTime, endDateTime, key);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<RectangleOperateParameterSettingController>(
      builder: (_) {
        return _listBody(context);
      },
    );
  }

  Widget _listBody(BuildContext context) {
    return Container(
      color: Colour.white,
      child: Column(
        children: <Widget>[
          Stack(
            children: <Widget>[
              BrnNormalFormGroup(
                title: 'siteMonitor.runningParameterSet'.tr,
                error: ResKey.errorTips.tr,
                isEdit: true,
                themeData: BrnFormItemConfig(
                    headTitleTextStyle: BrnTextStyle.withStyle(TextStyles.f18)),
                children: <Widget>[
                  ...controller.model.runningParameterList.map(
                    (item) => Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Expanded(
                          child: Container(
                            margin: EdgeInsets.only(left: 16.w),
                            child: BrnTextInputFormItem(
                              controller: item.textEditingController,
                              title: item.label,
                              hint: ResKey.plsInput.tr,
                              themeData: BrnFormItemConfig(
                                titleTextStyle: BrnTextStyle(
                                  fontSize: 14.sp,
                                ),
                                hintTextStyle: BrnTextStyle(
                                  fontSize: 14.sp,
                                ),
                              ),
                            ),
                          ),
                        ),
                        Padding(
                          padding: EdgeInsets.only(left: 5.w, right: 16.w),
                          child: Text(
                            item.unit,
                            style: TextStyle(
                              color: Colour.mainTextColor,
                              fontSize: 14.sp,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              Positioned(
                right: 16.w,
                child: GestureDetector(
                  onTap: () => controller.sendCommand(context),
                  child: Container(
                    alignment: Alignment.center,
                    padding: EdgeInsets.only(top: 15),
                    child: Text(
                      ResKey.deviceParameterSetting.tr, //参数设置
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        color: Colour.primaryColor,
                        fontSize: 16.sp,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
          ...controller.model.timeParameterMap.keys.map(
            (key) {
              TimeParamsItem item = controller.model.timeParameterMap[key]!;

              return Container(
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                height: 50,
                width: double.infinity,
                decoration: const BoxDecoration(
                  border: Border(
                    top: BorderSide(color: Colour.dividerColor),
                  ),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      item.label,
                      style: TextStyle(
                        fontSize: 14.sp,
                      ),
                    ),
                    GestureDetector(
                      onTap: () => _selectTime(context, key),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Container(
                            margin: EdgeInsets.only(left: 12.w, right: 4.w),
                            width: 56.w,
                            height: 20,
                            alignment: Alignment.center,
                            child: Text(
                              item.startHour == "" && item.startMinute == ""
                                  ? 'common.start'.tr //开始
                                  : "${item.startHour}:${item.startMinute}",
                              style: TextStyle(
                                color:
                                    item.endHour == "" && item.endMinute == ""
                                        ? Colour.hintTextColor
                                        : Colour.mainTextColor,
                              ),
                            ),
                          ),
                          const Text(
                            "－",
                            style: TextStyle(
                              color: Colour.lightTextColor,
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(left: 4.w, right: 22.w),
                            width: 56.w,
                            height: 20,
                            alignment: Alignment.center,
                            child: Text(
                              item.endHour == "" && item.endMinute == ""
                                  ? 'common.end'.tr
                                  : "${item.endHour}:${item.endMinute}",
                              style: TextStyle(
                                color:
                                    item.endHour == "" && item.endMinute == ""
                                        ? Colour.hintTextColor
                                        : Colour.mainTextColor,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(right: 10.w),
                      child: Text(
                        item.executionPower,
                        style: TextStyle(
                          fontSize: 14.sp,
                        ),
                      ),
                    ),
                    Expanded(
                      child: TextField(
                        controller: item.textEditingController,
                        decoration: InputDecoration(
                          hintText: ResKey.plsInput.tr,
                          // suffix:
                          hintStyle: TextStyle(
                            fontSize: 14.sp,
                            color: Colour.hintTextColor,
                          ),
                          border: InputBorder.none,
                        ),
                        keyboardType: TextInputType.number,
                        textAlign: TextAlign.end,
                        style: TextStyle(
                          color: Colour.mainTextColor,
                          fontSize: 14.sp,
                        ),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(
                        left: 4.w,
                      ),
                      child: Text(
                        item.unit,
                        style: TextStyle(
                          fontSize: 14.sp,
                        ),
                      ),
                    ),
                  ],
                ),
              );
            },
          ),
        ],
      ),
    );
  }
}
